package com.quicks.beauty.server.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.List;

@Service
public class RedisBillService {

    @Autowired
    @Qualifier(value = "redisTemplate")
    private RedisTemplate<String,Object> redisTemplate;

    public List<String> pushUserBillById(String userId,String billId){
        ListOperations<String, Object> forList = redisTemplate.opsForList();
        Long push = forList.rightPush(userId, billId);
        Charset utf8 = Charset.forName("UTF-8");
        byte[] chan = "bill".getBytes(utf8);
        byte[] bytes = (userId+"|"+billId).getBytes(utf8);
        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
        Long publish = connection.publish(chan, bytes);
        if(publish>0){
            connection.close();
        }

        return null;
    }
    public List<String> pushUserOrderById(String userId,String orderId){

        Charset utf8 = Charset.forName("UTF-8");
        byte[] chan = "order".getBytes(utf8);
        byte[] bytes = (userId+"|"+orderId).getBytes(utf8);
        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
        Long publish = connection.publish(chan, bytes);
        if(publish>0){
            connection.close();
        }

        return null;
    }
    public List<String> pushExpireOrder(String userId,String orderId){

        Charset utf8 = Charset.forName("UTF-8");
        byte[] chan = "pay".getBytes(utf8);
        byte[] bytes = (userId+"|"+orderId).getBytes(utf8);
        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
        Long publish = connection.publish(chan, bytes);
        if(publish>0){
            connection.close();
        }

        return null;
    }

    public Boolean delUserBillId(String userId){
        Boolean delete = redisTemplate.delete(userId);
        return delete;
    }


    public boolean getUserbill(String userId){
        Boolean hasKey = redisTemplate.hasKey(userId);
        return hasKey;
    }

}
